//
//  ZMYViewController.m
//  奇阅
//
//  Created by 奇阅科技 on 14-10-15.
//  Copyright (c) 2014年 奇阅科技. All rights reserved.
//

#import "ZMYViewController.h"
#import "ZMYRecommendCollectionViewCell.h"
#import "Config.h"
//#import "ZMYNetWorkAPI.h"
//#import "<#header#>"
@interface ZMYViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIAlertViewDelegate>
{
    NSMutableArray *dataArray;
}
@end

@implementation ZMYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    [self setupBookArray];
}
-(void)setupBookArray{
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    [activityIndicator setCenter:self.view.center];
    [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [self.view addSubview:activityIndicator];
    [activityIndicator startAnimating];
    
    [[ZMYNetWorkAPI default]getRecommend:^(NSArray *resultArray) {
        dataArray = [NSMutableArray arrayWithArray:resultArray];
        [self.collectionView reloadData];
        [activityIndicator stopAnimating];
    } error:^(NSString *errorStr) {
        [activityIndicator stopAnimating];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                        message:@"免费图书，网络不好未加载成功"
                                                       delegate:self
                                              cancelButtonTitle:@"下次再说"
                                              otherButtonTitles:@"拒绝显示",nil];
        [alert show];
    }];
}
-(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex==1) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isFirst"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    UIViewController *view = [[UIStoryboard storyboardWithName:@"BookRack" bundle:Nil] instantiateInitialViewController];
    view.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:view animated:YES completion:nil];
//    [AppDelegate delegate].window.rootViewController = view;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

-(void)viewWillDisappear:(BOOL)animated

{
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return dataArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ZMYRecommendCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZMYRecommendCollectionViewCell" forIndexPath:indexPath];
    BookRack *rack = dataArray[indexPath.item];
    if (indexPath.item<3) {
        rack.isSelect = YES;
    }
    cell.seleImageView.image = [UIImage imageNamed:(rack.isSelect?@"seleButton":@"noSeleButton")];
    [cell.bookCoverImageView setImageWithURL:[NSURL URLWithString:rack.book.imagefname]placeholderImage:[UIImage imageNamed:@"defaultCover"]];
    cell.bookTitleLabel.text = rack.book.title;
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ZMYRecommendCollectionViewCell *cell = (ZMYRecommendCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    BookRack *book = dataArray[indexPath.item];
    book.isSelect = !book.isSelect;
    cell.seleImageView.image = [UIImage imageNamed:(book.isSelect?@"seleButton":@"noSeleButton")];
}
- (IBAction)toBookRack:(id)sender {
    //下一步（将选择的书添加到书架）
    [MobClick event:@"2002"];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isFirst"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    UserAccount *user = [UserAccount currentAccount];
    for (BookRack *book in dataArray) {
        if (book.isSelect) {
            if (user) {
                book.uid = user.uid;
            }
            [[ZMYLKDB getLKDBHelper]insertBookRack:book];
        }
    }
    UIViewController *view = [[UIStoryboard storyboardWithName:@"BookRack" bundle:Nil] instantiateInitialViewController];
    view.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:view animated:YES completion:nil];
}
- (IBAction)skipTo:(id)sender {
    //跳过
    [MobClick event:@"2001"];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isFirst"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    UIViewController *view = [[UIStoryboard storyboardWithName:@"BookRack" bundle:Nil] instantiateInitialViewController];
    view.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:view animated:YES completion:nil];
}

@end
